/*
 * Copyright 2025 LinQingYing. and contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of this source code is governed by the Apache License 2.0,
 * which allows users to freely use, modify, and distribute the code,
 * provided they adhere to the terms of the license.
 *
 * The software is provided "as-is", and the authors are not responsible for
 * any damages or issues arising from its use.
 *
 */

package org.cangnova.cangjie.cjpm1.project.model.toml

import junit.framework.TestCase.assertEquals
import org.cangnova.cangjie.CangJieTestBase

class CjpmTomlMergerTest : CangJieTestBase(){
    

    fun `test merge package configs`() {
        val base = CjpmTomlConfig(
            `package` = PackageConfig(
                name = "base",
                version = "0.1.0",
                cjcVersion = "0.55.3",
                outputType = OutputType.DYNAMIC,
                description = "base description"
            )
        )
        
        val other = CjpmTomlConfig(
            `package` = PackageConfig(
                name = "other",
                version = "0.2.0",
                cjcVersion = "0.55.3",
                outputType = OutputType.STATIC,
                compileOption = "-O2"
            )
        )
        
        val merged = CjpmTomlMerger.merge(base, other)
        
        assertNotNull(merged.`package`)
        with(merged.`package`) {
            assertEquals("other", name)
            assertEquals("0.2.0", this?.version)
            assertEquals("0.55.3", this?.cjcVersion)
            assertEquals(OutputType.STATIC, this?.outputType)
            assertEquals("base description", this?.description)
            assertEquals("-O2", this?.compileOption)
        }
    }
    

    fun `test merge dependencies`() {
        val base = CjpmTomlConfig(
            dependencies = mapOf(
                "dep1" to DependencyConfig(path = "./dep1"),
                "dep2" to DependencyConfig(git = "https://example.com/dep2.git")
            )
        )
        
        val other = CjpmTomlConfig(
            dependencies = mapOf(
                "dep2" to DependencyConfig(git = "https://example.com/dep2-new.git"),
                "dep3" to DependencyConfig(path = "./dep3")
            )
        )
        
        val merged = CjpmTomlMerger.merge(base, other)
        
        assertEquals(3, merged.dependencies.size)
        assertEquals("./dep1", merged.dependencies["dep1"]?.path)
        assertEquals("https://example.com/dep2-new.git", merged.dependencies["dep2"]?.git)
        assertEquals("./dep3", merged.dependencies["dep3"]?.path)
    }
    

    fun `test merge target configs`() {
        val base = CjpmTomlConfig(
            target = mapOf(
                "x86_64-unknown-linux-gnu" to TargetConfig(
                    compileOption = "-O2",
                    binDependencies = BinDependenciesConfig(
                        pathOption = listOf("/usr/lib")
                    )
                )
            )
        )
        
        val other = CjpmTomlConfig(
            target = mapOf(
                "x86_64-unknown-linux-gnu" to TargetConfig(
                    compileOption = "-O3",
                    binDependencies = BinDependenciesConfig(
                        pathOption = listOf("/opt/lib")
                    )
                )
            )
        )
        
        val merged = CjpmTomlMerger.merge(base, other)
        
        val targetConfig = merged.target["x86_64-unknown-linux-gnu"]
        assertNotNull(targetConfig)
        assertEquals("-O3", targetConfig?.compileOption)
        assertEquals(2, targetConfig?.binDependencies?.pathOption?.size)
        assertEquals(listOf("/usr/lib", "/opt/lib"), targetConfig?.binDependencies?.pathOption)
    }
} 